<template>
	<view class="register">
	
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体 -->
			<view class="main">
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="手机号"
				></wInput>
				
				<wInput
					v-model="nickname"
					type="text"
					maxlength="11"
					placeholder="昵称"
				></wInput>
				
				<wInput
					v-model="email"
					type="text"
					maxlength="11"
					placeholder="email"
				></wInput>
				
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="登录密码"
					isShowPass
				></wInput>
<!-- 				<wInput
					v-model="verCode"
					type="number"
					maxlength="4"
					placeholder="验证码"
					
					isShowCode
					ref="runCode"
					@setCode="getVerCode()"
				></wInput> -->
					
			</view>
				
			<wButton 
				class="wbutton"
				text="注 册"
				:rotate="isRotate" 
				@click.native="startReg()"
			></wButton>
			
			<!-- 底部信息 -->
			<view class="footer">
				<text 
					@tap="isShowAgree" 
					class="cuIcon"
					:class="showAgree?'cuIcon-radiobox':'cuIcon-round'"
				> 同意</text>
				<!-- 协议地址 -->
				<navigator url="" open-type="navigate">《协议》</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	
	export default {
		data() {
			return {
				nickname :'',//昵称
				email:'',//email
				//logo图片 base64
				logoImage: '',
				phoneData:'', // 用户/电话
				passData:'', //密码
				verCode:"1234", //验证码
				showAgree:true, //协议是否选择
				isRotate: false, //是否加载旋转
			}
		},
		components:{
			wInput,
			wButton,
		},
		mounted() {
			_this= this;
		},
		methods: {
			isShowAgree(){
				//是否选择协议
				_this.showAgree = !_this.showAgree;
			},
			getVerCode(){
				//获取验证码
				if (_this.phoneData.length != 11) {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不正确'
				    });
				    return false;
				}
				console.log("获取验证码")
				this.$refs.runCode.$emit('runCode'); //触发倒计时（一般用于请求成功验证码后调用）
				uni.showToast({
				    icon: 'none',
					position: 'bottom',
				    title: '模拟倒计时触发'
				});
				
				setTimeout(function(){
					_this.$refs.runCode.$emit('runCode',0); //假装模拟下需要 终止倒计时
					uni.showToast({
					    icon: 'none',
						position: 'bottom',
					    title: '模拟倒计时终止'
					});
				},3000)
			},
		   async startReg() {
				//注册
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.showAgree == false) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '请先同意《协议》'
				    });
				    return false;
				}
				if (this.phoneData.length !=11) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不正确'
				    });
				    return false;
				}
		        if (this.passData.length < 1) {
		            uni.showToast({
		                icon: 'none',
						position: 'bottom',
		                title: '密码不正确'
		            });
		            return false;
		        }
				if (this.verCode.length != 4) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '验证码不正确'
				    });
				    return false;
				};
				
				if (this.email.length < 4) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: 'email填写不正确'
				    });
				    return false;
				};
				if (this.nickname.length < 1) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: 'nickname填写不正确'
				    });
				    return false;
				};
				console.log("页面注册信息填写验证成功");
				const res = await this.$myRuquest({  //校验手机号是否存在
					url: '/api/login_mix',
					method:'POST',
					data:
						{username:this.phoneData}
				});
				if(res.data.mobile!=0){
					this.$api.msg(this.phoneData+" 已经注册过了");
					return false;
				}
				let logo=['https://vkceyugu.cdn.bspapp.com/VKCEYUGU-aaf91478-24aa-44ca-8e18-db104a801dea/c69c7cd9-859a-4dfe-8910-55b0f9ee07dc.png',
				'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-aaf91478-24aa-44ca-8e18-db104a801dea/6c646dd2-0e7c-4d0d-b309-ae20b975d58b.png',
				'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-aaf91478-24aa-44ca-8e18-db104a801dea/6ca64611-24fa-4ac3-9588-3fef4bed5995.png',
				'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-aaf91478-24aa-44ca-8e18-db104a801dea/2796d6a8-0c98-440a-9db6-be96ae4276dc.png',
				'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-aaf91478-24aa-44ca-8e18-db104a801dea/3d088eb2-9e4b-4574-b0e3-c18f8f1d2d0b.png',
				'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-aaf91478-24aa-44ca-8e18-db104a801dea/e2cf5a1f-04c6-4763-918d-8f85be575070.png',
				'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-aaf91478-24aa-44ca-8e18-db104a801dea/2c577922-de26-49ab-ac0f-261af831d78d.png',
				]
				let i=Math.floor(Math.random() * (6 - 0)) + 0;
				let portrait=logo[i];
				let req={
				"id":0,
				"mobile":this.phoneData,
				"nickname":this.nickname,
				"email":this.email,
				"password":this.passData,
				"portrait":portrait,
				}
				console.log(" register req---",req)
				const res_insert = await this.$myRuquest({
					url: '/api/login_mix_insert',
					method:'POST',
					// data:{"obj":req},
					data:req,
				});
				console.log("register.vue res_insert.data.mobile ",res_insert.data.mobile!=0)
				if(res_insert.data.mobile!=0){
					this.$api.msg(this.phoneData+" 注册成功");
					// return false;
					uni.navigateBack();  
				}
				else{
					this.$api.msg(this.phoneData+" 注册失败");
				}
				
				
				_this.isRotate=true
				setTimeout(function(){
					_this.isRotate=false
				},3000)
		    }
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	/* @import url("./css/main.css"); */
	
	
	
	.content {
		display: flex;
		flex-direction: column;
		justify-content:center;
		/* margin-top: 128rpx; */
	}
	
	/* 头部 logo */
	.header {
		width:161rpx;
		height:161rpx;
		box-shadow:0rpx 0rpx 60rpx 0rpx rgba(0,0,0,0.1);
		border-radius:50%;
		background-color: #000000; 
		margin-top: 128rpx;
		margin-bottom: 72rpx;
		margin-left: auto;
		margin-right: auto;
	}
	.header image{
		width:161rpx;
		height:161rpx;
		border-radius:50%;
	}
	
	/* 主体 */
	.main {
		display: flex;
		flex-direction: column;
		padding-left: 70rpx;
		padding-right: 70rpx;
	}
	.tips {
		color: #999999;
		font-size: 28rpx;
		margin-top: 64rpx;
		margin-left: 48rpx;
	}
	
	/* 登录按钮 */
	.wbutton{
		margin-top: 96rpx;
	}
	
	/* 其他登录方式 */
	.other_login{
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		margin-top: 256rpx;
		text-align: center;
	}
	.login_icon{
		border: none;
		font-size: 64rpx;
		margin: 0 64rpx 0 64rpx;
		color: rgba(0,0,0,0.7)
	}
	.wechat_color{
		color: #83DC42;
	}
	.weibo_color{
		color: #F9221D;
	}
	.github_color{
		color: #24292E;
	}
	
	/* 底部 */
	.footer{
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		font-size: 28rpx;
		margin-top: 64rpx;
		color: rgba(0,0,0,0.7);
		text-align: center;
		height: 40rpx;
		line-height: 40rpx;
	}
	.footer text{
		font-size: 24rpx;
		margin-left: 15rpx;
		margin-right: 15rpx;
	}
</style>